/*$(function(){
	//登陆按钮绑定回车键
	$(document).keydown(function(event){
		if(event.keyCode==13){
			$("#login").click();
		}
	});
})*/
/*//登录
function toLogin(){
	if(phone!="" && password!=""){
		$.ajax({
			url: urlcore + "
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				if (data.success == true) {					
					//设置cookie
			var domain_url = urlcore.split('://')[1];
			if (domain_url.indexOf(':') > -1) {
				domain_url = domain_url.split(':')[0];
			}else{
				domain_url = domain_url.split('/')[0];
			}
				var str="JSESSIONID="+encodeURIComponent(data.data.token);
				var esp=new Date();
				esp.setTime(esp.getTime()+7*24*60*60*1000);
				str+=";expires="+esp.toGMTString();
				document.cookie=str;
			    window.location.href = document.referrer
				/*	var domain_url = urlcore.split('://')[1];
					if (domain_url.indexOf(':') > -1) {
						domain_url = domain_url.split(':')[0];
					}else{
						domain_url = domain_url.split('/')[0];
					}
					document.cookie = "JSESSIONID="+data.data.token+";path=/;domain="+domain_url;
					window.location.href="index.html";
				} else {
					alert(data.msg);
				}
			},
			error:function() {
				/* Act on the event
				alert("error");
			}
		});
	} 
}
*/

//登录
$(function(){
	var  wxName='';
  var   headImg= '';
  var   isLogin= false;
   var  bullLevel= 1;
	getUserInfoByUserId();
	// delete1();
	if(getvl("code")!=""){
	if(sessionStorage.getItem("phone")!="" && sessionStorage.getItem("phossne")!=""){
		 $.ajax({
				url: urlcore + "/api/wxupdate?codess="+getvl("code"),//
				type: "post",
				contentType: "application/json;charset=utf-8",
				data: JSON.stringify({ //所在省市
					"password": sessionStorage.getItem("phossne"),
					"phone": sessionStorage.getItem("phone")
				}),
//           data: {
//         phone: client_phone,
//         code: veri_code
//       },
				dataType: "json",
				success: function(data) {
					// console.log(1111,data)
					//data是返回的hash,key之类的值，key是定义的文件名
					//alert(JSON.stringify(data.data));
					//	window.location.href="8.0.0zhaohuo.html";
					if(data.success == true) {
						//alert(JSON.stringify(data.data.jsonObject));
			          //toLogin(data.data.phone,data.data.password);
                    window.location.href = "gotoindex.html";
					} else{
					alert(data.msg);
					 window.location.href = "gotoindex.html";
				    }
				},
				error: function() {
					alert("修改失败");
				}
			});
	}else{
		alert("超时");
		window.history.go(-1);
	}
		}
});
//登陆 
function denglu() {
	// var status=false;
	var phone = $('#phossne1').val().trim();
	sessionStorage.setItem('phone',phone)
	if(phone == '') {
		layer.alert("请输入手机号码！");
		return false;
	}
	var password = $('#phossne').val().trim();
	sessionStorage.setItem('password',password)
	if(password == '') {
		layer.alert("请输入登录密码！");
		return false;
	}
	$.ajax({
		url: urlcore + "/api/user/loginByPhone",
		type: "post",
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify({
			"phone": phone,
			"password": password
		}),
		dataType: "json",
		success: function(data) {
			   sessionStorage.setItem('userIdd',data.data.id);
					if(data.success == true) {
						if(layer.confirm('登录成功')) {
							getUserInfoByUserId();
							var da = data.data; 
							if(window.sessionStorage) {
								//设置cookie
								var domain_url = urlcore.split('://')[1];
								if(domain_url.indexOf(':') > -1) {
									domain_url = domain_url.split(':')[0];
								} else {
									domain_url = domain_url.split('/')[0];
								}
								document.cookie = "JSESSIONID=" + data.data.token + ";path=/;domain=" + domain_url;
								window.location.href ="buy/12.0.0buy_wode.html";
								
							} else {
								alert("浏览暂不支持sessionStorage");
							}
						} else {
							//跳转列表页
							window.location.href = "1.0.0denglu.html";
						}
					} else if(data.code == 'OVERTIME') {
						var thisUrl = window.location.href;
						if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
							top.window.location.href = "1.0.0denglu.html";
						}
					} else {
						alert(data.msg);
					}
		},
		error: function() {
			alert("error");
		}
				
	});
}
//获取买家信息 
function getUserInfoByUserId() {
	$("#dengluxinxi").html("");
	$.ajax({
		url: urlcore + "/api/user/getUserInfo",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			console.log(data)
			if(data.success == true) {
				var da = data.data;
				sessionStorage.setItem("userId", da.id);
				sessionStorage.setItem("userName", da.userName);
				//ryToken
				sessionStorage.setItem("ryToken", da.ryToken);
				if(da.userName != '' && da.userName != null) {
					if(da.userType != '' && da.userType != null && da.userType == 1) {
						$('#shopManager').css("display", "none");
					}
					$('#register1').css("display", "none");
					$('#login1').css("display", "none");
					$('#userName1').css("display", "block");
					$('#logout1').css("display", "block");
					$("#userName").text(da.userName);

				}
         headImg=da.headImg;
				 wxName=da.userName;
				 bullLevel=da.bullLevel;
				 isLogin=true;
				var thislist ='<dl class="clearfix">'+
							'<dt>'+
									'<a href="12.6.0gerenziliao.html?id='+da.id+'">'+
											'<img onerror="errorImg(this)" style="margin-top:-106px" src="'+headImg+'">'+
									'</a>'+
							'</dt>'+
							'<dd style="margin-left:-20px;">'+
								//<!--已登录-->
									'<h5>'+
											'<a href="12.6.0gerenziliao.html?id='+da.id+'">'+wxName+'</a>'+
									'</h5>'+
									'<h4><a href="12.6.0gerenziliao.html?id='+da.id+'">LV'+bullLevel+'</a></h4>'+
							'</dd>'+
							'<dd style="margin-left:30px;border:1px solid #fff;height:28px;width:50px;"><button onclick="logout()" style="margin-top:-132px;display:inline-block;width:100%;height:100%;color:#fff;text-align:center">注销</button></dd>'+
					'</dl>';
				$('#dengluxinxi').append(thislist);
			}else{
				console.log(123)
				var dengluxinxi='<ol class="clearfix">'+
				                '<li>'+
												  '<a  href="javascript:void(0)" class="buy_wode_denglu aa1" style="border:0"><img src="../images/add/me.png" style="width:56px; height:56px;border-radius:50%" /></a>'+
				                	'<a   href="../1.0.0denglu.html" class="buy_wode_denglu aa2">登陆</a>'+
													'<a  href="../1.3.0zhuce.html" class="buy_wode_denglu aa3">注册</a>'+
				                '</li>'+
				            '</ol>';
				 $("#dengluxinxi").append(dengluxinxi); 
			}
		},
		error: function() {
			layer.alert("error");
		}
	});
}
// 注销成功
function getUserInfoByUserId1() {
	$("#dengluxinxi").html("");
	$.ajax({
		url: urlcore + "/api/user/getUserInfo",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var da = data.data;
				sessionStorage.setItem("userId", da.id);
				sessionStorage.setItem("userName", da.userName);
				//ryToken
				sessionStorage.setItem("ryToken", da.ryToken);
				if(da.userName != '' && da.userName != null) {
					if(da.userType != '' && da.userType != null && da.userType == 1) {
						$('#shopManager').css("display", "none");
					}
					$('#register1').css("display", "none");
					$('#login1').css("display", "none");
					$('#userName1').css("display", "block");
					$('#logout1').css("display", "block");
					$("#userName").text(da.userName);

				}
         headImg=da.headImg;
				 wxName=da.userName;
				 bullLevel=da.bullLevel;
				 isLogin=true;
				var dengluxinxi='<ol class="clearfix">'+
				                '<li>'+
												  '<a  href="javascript:void(0)" class="buy_wode_denglu aa1" style="border:0"><img src="../images/add/me.png" style="width:56px; height:56px;border-radius:50%" /></a>'+
				                	'<a   href="../1.0.0denglu.html" class="buy_wode_denglu aa2">登陆</a>'+
													'<a  href="../1.3.0zhuce.html" class="buy_wode_denglu aa3">注册</a>'+
				                '</li>'+
				            '</ol>';
				$('#dengluxinxi').append(dengluxinxi);
			}else{
				var dengluxinxi='<ol class="clearfix">'+
				                '<li>'+
												  '<a  href="javascript:void(0)" class="buy_wode_denglu aa1" style="border:0"><img src="../images/add/me.png" style="width:56px; height:56px;border-radius:50%" /></a>'+
				                	'<a   href="../1.0.0denglu.html" class="buy_wode_denglu aa2">登陆</a>'+
													'<a  href="../1.3.0zhuce.html" class="buy_wode_denglu aa3">注册</a>'+
				                '</li>'+
				            '</ol>';
				 $("#dengluxinxi").append(dengluxinxi); 
			}
		},
		error: function() {
			layer.alert("error");
		}
	});
}
function delete1() {
	var that=this;
	var delId = sessionStorage.getItem('id');//选中id
	// console.log(delId)
	// if(alert('确定删除改商品吗？')){
		$.ajax({
			url: urlcore + '/api/goodsCar/delGoodsCarByGoodsIdOnPC?ids=' + delId,
			type: "get",
			contentType: "application/json;charset=utf-8",
			data:{},
			dataType: "json",
			success: function(json) {
			console.log(222,json)
				if (json.success) {
					//更新商品
					alert('删除成功')
					// that.selectGoodsCarList();
				}
			}
		})
	// }			
}
//注销
function logout() {
	//删除回话
	sessionStorage.removeItem("userId");
	$.ajax({
		url: urlcore + "/api/login/logout",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				
				headImg='';
				wxName='';
				bullLevel='';
				isLogin=false;
				bullLevel= 1;
				 //window.location.href = "../buy/12.0.0buy_wode.html";
				 sessionStorage.setItem('userName','');
				 sessionStorage.setItem('ryToken','');
				 getUserInfoByUserId1() 
			} else {
				layer.alert(data.msg);
			}
		},
		error: function() {
			/* Act on the event */
			layer.alert("error");
		}
	});
}